package com.manna.library_aspectj

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
import org.gradle.api.Plugin
import org.gradle.api.Project


class AspectjPlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
        def hasApp = project.plugins.withType(AppPlugin)
        def hasLib = project.plugins.withType(LibraryPlugin)

        if (!hasApp && !hasLib) {
            throw new IllegalStateException("'android' or 'android-library' plugin required.")
        }

        final def log = project.logger
        final def variants
        if (hasApp) {
            variants = project.android.applicationVariants
            log.error "========================"
            log.error "module引用"
            log.error "========================"
        } else {
            variants = project.android.libraryVariants
            log.error "========================"
            log.error "library引用"
            log.error "========================"
        }
        log.error "========================"
        log.error "精简的Plugin，开始修改Class!"
        log.error "========================"
        project.buildscript {
            repositories {
                mavenCentral()
            }
            dependencies {
                classpath 'org.aspectj:aspectjtools:1.8.9'
            }
        }

        project.dependencies {
            implementation 'org.aspectj:aspectjrt:1.8.9'
        }

        variants.all { variant ->

            variant.javaCompileProvider.configure {
                it.doLast {
                    String[] args = [
                            "-showWeaveInfo",
                            "-1.8",
                            "-inpath", destinationDir.toString(),
                            "-aspectpath", classpath.asPath,
                            "-d", destinationDir.toString(),
                            "-classpath", classpath.asPath,
                            "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
                    ]

                    log.debug "ajc args: " + Arrays.toString(args)

                    MessageHandler handler = new MessageHandler(true)
                    new Main().run(args, handler)

                    for (IMessage message : handler.getMessages(null, true)) {
                        switch (message.getKind()) {
                            case IMessage.ABORT:
                            case IMessage.ERROR:
                            case IMessage.FAIL:
                                log.error message.message, message.thrown
                                break
                            case IMessage.WARNING:
                            case IMessage.INFO:
                                log.info message.message, message.thrown
                                break
                            case IMessage.DEBUG:
                                log.debug message.message, message.thrown
                                break
                        }
                    }
                }
            }
        }
    }
}